<?php


namespace app\common\service\agent;


use app\model\CouponBrand;
use think\facade\Cache;


class showBrandByJumpTypeService
{
    use \app\api\controller\traits\coupon_order\Coupon;

    public function showBrandByJumpType($jump_type, $brandName)
    {
        $list = array();
        $jh = array();
        $jh['id']     =  0;
        $jh['name']   =  '聚合页';
        $couponBrand = new CouponBrand();
        //周边游10
        //出行/加油-----9
        //连锁餐饮-----11
        $cache_key = 'showBrandByJumpTypeService_showBrandByJumpType_getBrand_'.$jump_type;
        if (in_array($jump_type, [PRODUCT_TRAVEL_OIL_SERV, PRODUCT_FOOD_SHOP_SERV])) {
            //TODO 由于属于聚推客的内容，所以变动相对来说较小，可以添加缓存
            $list =  Cache::get($cache_key);
            if ($list) {
                return success('查询成功', $list);
            } else {
                $list = $couponBrand->getJutuikelList([
                    'name' => $brandName,
                    'status' => CouponBrand::STATUS_ACTIVE,
                    'serv_type' => $jump_type,
                    'field' => 'act_id, act_name, name, image_url, type, serv_type'
                ])->toArray();
                if ($jump_type == PRODUCT_TRAVEL_OIL_SERV) { //连锁餐饮没有聚合页
                    array_unshift($list, $jh);
                }
                Cache::set($cache_key, $list, 2*3600);
            }
        } else {
            //权益卡--------5----3
            if ($jump_type == 5) {
                //TODO 由于系统的品牌权益随时会发生变化，不能使用缓存
                $list = array();
                $result = json_decode(($this->getCateAndSubBrand())->getContent(),true);
                foreach ($result['data'] as $cateBrand) {
                    foreach ($cateBrand['couponBrand'] as $couponBrand) {
                        if ($couponBrand['serv_type'] > 0) {
                            continue;
                        }
                        $list[$couponBrand['id']]['id']   = $couponBrand['id'];
                        $list[$couponBrand['id']]['name'] = $couponBrand['name'];
                        $list[$couponBrand['id']]['image_url'] = $couponBrand['image_url'];
                    }
                }
                $list = array_values($list);
                if (!empty($brandName)) {
                    $list = array_filter($list, function($t) use ($brandName) {
                        return $t['name'] == $brandName;
                    });
                }
                array_unshift($list, $jh);
            }
        }
        return success('查询成功', $list);
    }
}